local __is_dev_env__ = commonlib.__is_dev_env__;
local __tab__ = "    "
local __lua_print__ = print;
-- local __log_debug__ = commonlib.__log_debug__;
-- local __log_info__ = commonlib.__log_info__;
-- local __log_warn__ = commonlib.__log_warn__;
-- local __log_error__ = commonlib.__log_error__;

local function __log_debug__(msg)
    commonlib.__log_debug__(msg);
    commonlib.logger:debug(msg);
end
local function __log_info__(msg)
    commonlib.__log_info__(msg);
    commonlib.logger:info(msg);
end
local function __log_warn__(msg)
    commonlib.__log_warn__(msg);
    commonlib.logger:warn(msg);
end
local function __log_error__(msg)
    commonlib.__log_error__(msg);
    commonlib.logger:error(msg);
end

local function GetLocationInfo(level)
	local info = debug.getinfo(level or 1, "nSl")
	if not info then return "" end
	
	if info.what == "C" then return "C function" end
    local filepath = info.source;
    filepath = string.sub(filepath, 1, string.find(info.source, "\n", 1, true));
    filepath = string.gsub(filepath, "%s*$", "");
    filepath = string.gsub(filepath, "^%s*", "");
    return string.format("%s:%d %s", filepath, info.currentline, info.name or "");
end

local function ToString(val, key, indent, output_table, text)
    indent = indent or "";
    output_table = output_table or {};
    text = text and (text .. "\n") or "";

    if (type(val) ~= "table" or output_table[val]) then
        if (key and key ~= "") then
            text = text .. string.format("%s%s = %s", indent, tostring(key), tostring(val));
        else
            text = text .. string.format("%s%s", indent, tostring(val));
        end
        return text;
    end

    if (key and key ~= "") then
        text = text .. string.format("%s%s = %s {", indent, key, tostring(val));
    else
        text = text .. string.format("%s%s {", indent, tostring(val));
    end

    output_table[val] = true;  -- 表记已输出

    for k, v in pairs(val) do
        text = text .. "\n" .. ToString(v, k, indent .. __tab__, output_table);
    end

    text = text .. "\n" .. string.format("%s}", indent);

    return text;
end

local function Print(output, dept, ...)
    local filepos = __is_dev_env__ and GetLocationInfo(dept or 3) or "";
    local args = table.pack(...);
    local datetime = os.date("%Y-%m-%d %H:%M:%S");
    local text = string.format("[%s][%s]", datetime, filepos);
    for i = 1, args.n do text = text .. __tab__ .. ToString(args[i]) end
    (output or __lua_print__)(text);
end

local __print__ = __log_debug__; -- __lua_print__ __log_debug__
commonlib.tostring = ToString;
commonlib.print = function(...)
    Print(__print__, 4, ...);
end
commonlib.print_if = function(is_print, ...)
    if (is_print) then Print(__print__, 4, ...) end
end
commonlib.print_format = function(...)
    Print(__print__, 4, string.format(...));
end
commonlib.echo = commonlib.print;
commonlib.echo_if = commonlib.print_if;

commonlib.debug = function(...)
    Print(__log_debug__, 4, ...);
end
commonlib.info = function(...)
    Print(__log_info__, 4, ...);
end
commonlib.warn = function(...)
    Print(__log_warn__, 4, ...);
end
commonlib.error = function(...)
    Print(__log_error__, 4, ...);
end

-- _G.print = commonlib.print;
return Log;
